<template>
    <div class="main">
        <div style="width: 100%;height: 30px;display: flex;">
            <div style="width: 250px; display: flex">
                <el-button type="primary" size="mini" round @click="clean">清空购物车</el-button>
                <el-button type="info" size="mini" round @click="submit">提交订单</el-button>
            </div>
        </div>

        <div v-if="userAll">
            <el-table :data="list" v-loading="queryLoading" style="width: 100%;
      height: 76vh;" :default-sort="{ prop: 'date', order: 'descending' }" key="table1">
                <el-table-column type="selection" width="55px" v-if="checkBox">
                </el-table-column>
                <el-table-column label="ID" prop="id" key="id">
                    <template slot-scope="scope">
                        <span style="">{{ scope.row.id }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="菜品名字" prop="name" align="center">
                    <template slot-scope="scope">
                        <span style="">{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="分类ID" prop="dishId" sortable align="center">
                    <template slot-scope="scope">
                        <span style="">{{ scope.row.dishId }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="价格" prop="amount" align="center">
                    <template slot-scope="scope">
                        <span style="">{{ scope.row.amount }}￥</span>
                    </template>
                </el-table-column>
                <el-table-column label="图片" prop="image" sortable align="center">
                    <template slot-scope="scope">
                        <div style="width: 100px;height: 50px;">
                            <el-image :src="scope.row.image" lazy></el-image>
                        </div>
                        <!-- <span style="">{{ scope.row.image }}</span> -->
                    </template>
                </el-table-column>
                <el-table-column label="数量" prop="number" sortable align="center" width="190px">
                    <template slot-scope="scope">
                        <span style=""> {{ scope.row.number }}</span>
                        <!-- <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" active-text="起售" inactive-text="停售"
                active-color="#13ce66" inactive-color="#ff4949" @change="dishStatus" ref="status">
              </el-switch> -->
                    </template>
                </el-table-column>
                <el-table-column align="center" width="150px" label="操作">
                    <template slot-scope="scope">
                        <!-- <el-button size="medium" type="primary" @click="handleAdd(scope.$index, scope.row)"
                style="width: 60px;height: 40px;">加入购物车</el-button> -->
                        <el-button size="medium" type="danger" @click="handleDelete(scope.$index, scope.row)"
                            style="width: 60px;height: 40px;">移除购物车</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <userEditView :dialogVisible.sync="dialogVisible" :dialogInfo.sync="dialogInfo"></userEditView>
        <userDeleteView :dialogVisible_delete.sync="dialogVisible_delete" :dialogInfo_delete.sync="dialogInfo_delete">
        </userDeleteView>
        <el-pagination :total="pagination.total" :current-page.sync="pagination.currentPage"
            :page-size.sync="pagination.PageSize" :page-sizes="[6]" layout="total, sizes, prev, pager, next, jumper"
            @size-change="query()" @current-change="query()" />
    </div>
</template>
<script>
import { METHOD, request } from '@/utils/request'
import { Message } from 'element-ui'
// import Axios from 'axios'
export default {
  name: 'userDishView',
  components: {
  },
  created () {
    this.query()
  },
  watch: {
    dialogInfo: {
      handler (newVal, oldVal) {
        const params = JSON.stringify(newVal)
        console.log(params)
        this.Editquery(params)
        // this.query()
      }
    }
  },
  // updated () {
  //   this.query()
  // },
  computed () {
    console.log(this.input)
  },
  mounted () {
  },
  data () {
    return {
      userAll: true,
      list: {},
      queryLoading: true, // 查询加载状态
      pagination: {
        // 分页数据
        total: 0, // 记录总数
        currentPage: 1, // 当前页
        PageSize: 6, // 每页的记录条数
        sort: 'updateAt,desc' // 查询排序依据
      }
    }
  },
  methods: {
    change () {
      this.$forceUpdate()
    },
    query () {
      this.userAll = true
      this.queryLoading = true
      this.userSearch = false
      const params = Object.assign(
        {
          page: this.pagination.currentPage,
          pageSize: this.pagination.PageSize
        })
      request('/api/user/shoppingCart/list', METHOD.POST, params).then((resp) => {
        this.list = resp.data.data.records
        this.pagination.total = resp.data.data.total
        this.currentPage = resp.data.data.currentPage
        this.pageSize = resp.data.data.pageSize
        this.queryLoading = false
        console.log(resp.data.data)
      })
    },
    // handleAdd (index, row) {
    //   const params = { dishId: row.id }
    //   console.log(row)
    //   request('/api/user/shoppingCart/add', METHOD.POST, params).then((resp) => {
    //     if (resp.status === 200) {
    //       Message({
    //         message: '加入购物车成功!',
    //         type: 'success',
    //         duration: 5 * 1000
    //       })
    //     }
    //   })
    // },
    handleDelete (x, y) {
      const params = { dishId: y.dishId }
      const constant = JSON.stringify(params)
      request('/api/user/shoppingCart/sub', METHOD.POST, constant).then((resp) => {
        if (resp.status === 200) {
          Message({
            message: '删除菜品成功!',
            type: 'success',
            duration: 5 * 1000
          })
          this.query()
        }
      }).finally()
    },
    clean () {
      request('/api/user/shoppingCart/clean', METHOD.DELETE).then((resp) => {
        if (resp.status === 200) {
          Message({
            message: '清空购物车成功!',
            type: 'success',
            duration: 5 * 1000
          })
          this.query()
        }
      })
    },
    submit () {
      const value = this.list
      let sum = 0
      for (let i = 0; i < value.length; i++) {
        sum += value[i].amount
      }
      const params = {
        addressBookId: 2,
        remark: '订单',
        amount: sum
      }
      request('/api/user/shoppingCart/submit', METHOD.POST, params).then((resp) => {
        if (resp.status === 200) {
          Message({
            message: '提交订单!',
            type: 'success',
            duration: 5 * 1000
          })
        }
      })
    },
    dishStatus () {
      console.log(this.$refs.status.value)
    },
    categoryIdSearch () {

    }
  }
}
</script>
<style scoped>
* {
    margin: 0;
    padding: 0;
}

.el-pagination {
    margin-left: 25%;
    width: 200px;
    height: 50px;
}
</style>
